//
//  AddUserViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/8/1.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "AddUserViewController.h"
#import "PLTextView.h"
#import "ImageSelectEditorView.h"

@interface AddUserViewController ()<UITextFieldDelegate,UITextViewDelegate>{
    UIScrollView *scrollBG;
    UITextField *txtName;//姓名
    UITextField *txtTel;//资本
    UITextField *txtWx;
    UITextField *txtEmail;
    UITextField *txtCompany;
    UITextField *txtCode;
    PLTextView *txtContent;
    ImageSelectEditorView *imageEditorV;
    
    NSInteger tempY;
}

@end

@implementation AddUserViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    
//    KKNavigationController *kNav=(KKNavigationController *)self.navigationController;
//    kNav.canDragBack=NO;
    //注册监听键盘事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    
//    KKNavigationController *kNav=(KKNavigationController *)self.navigationController;
//    kNav.canDragBack=YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    scrollBG=[[UIScrollView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, kContentHeight-44)];
    scrollBG.backgroundColor=rgbGray;
    scrollBG.autoresizingMask=UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:scrollBG];
    
    UIButton *btnSub=[RHMethods buttonWithFrame:CGRectMake(0, YH(scrollBG), kScreenWidth, 44) title:@"发布" image:nil bgimage:nil];
    [btnSub setBackgroundColor:rgbpublicColor];
    [btnSub setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnSub addTarget:self action:@selector(subButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnSub];
    
    tempY=10;
    txtName=[self addViewWithFrameY:tempY withTitle:@"姓名" withSel:nil];
    tempY+=44;
    txtTel=[self addViewWithFrameY:tempY withTitle:@"手机" withSel:nil];
    tempY+=44;
    txtWx=[self addViewWithFrameY:tempY withTitle:@"微信" withSel:nil];
    tempY+=44;
    if ([self.userInfo isEqualToString:@"user"] ||
        [self.userInfo isEqualToString:@"userEdit"]) {
        txtEmail=[self addViewWithFrameY:tempY withTitle:@"邮箱" withSel:nil];
        tempY+=44;
        txtCompany=[self addViewWithFrameY:tempY withTitle:@"公司名称" withSel:nil];
        tempY+=44+10;
    }else{
        txtCode=[self addViewWithFrameY:tempY withTitle:@"合同编号" withSel:nil];
        tempY+=44+10;
    }
    
    UIView *viewBG=[[UIView alloc] initWithFrame:CGRectMake(0, tempY, kScreenWidth, 110)];
    viewBG.backgroundColor=[UIColor whiteColor];
    [scrollBG addSubview:viewBG];
    txtContent=[[PLTextView alloc] initWithFrame:CGRectMake(10, tempY+5, kScreenWidth-20, 100)];
    txtContent.delegate=self;
    txtContent.Placeholder=@"请填写备注信息";
    [scrollBG addSubview:txtContent];
    tempY+=110;
    [self initImagesView];
    NSMutableArray *arrayImageT=[NSMutableArray new];
    if ([self.userInfo isEqualToString:@"userEdit"]) {
        txtName.text=[self.otherInfo valueForJSONStrKey:@"name"];
        txtTel.text=[self.otherInfo valueForJSONStrKey:@"mobile"];
        txtWx.text=[self.otherInfo valueForJSONStrKey:@"wechat"];
        txtEmail.text=[self.otherInfo valueForJSONStrKey:@"email"];
        txtCompany.text=[self.otherInfo valueForJSONStrKey:@"companyname"];
        txtContent.text=[self.otherInfo valueForJSONStrKey:@"descr"];
        [arrayImageT addObjectsFromArray:[self.otherInfo objectForJSONKey:@"path"]];
    }else if([self.userInfo isEqualToString:@"sellEdit"]){
        txtName.text=[self.otherInfo valueForJSONStrKey:@"name"];
        txtTel.text=[self.otherInfo valueForJSONStrKey:@"mobile"];
        txtWx.text=[self.otherInfo valueForJSONStrKey:@"wechat"];
        txtCode.text=[self.otherInfo valueForJSONStrKey:@"contract"];
        txtContent.text=[self.otherInfo valueForJSONStrKey:@"descr"];
        [arrayImageT addObjectsFromArray:[self.otherInfo objectForJSONKey:@"path"]];
    }
    
    __weak UIScrollView *viewS=scrollBG;
    [imageEditorV setImageUrlArray:arrayImageT imageChange:^(NSInteger suc,CGFloat h) {
        viewS.contentSize=CGSizeMake(kScreenWidth-1, YH(imageEditorV)>H(viewS)+1?YH(imageEditorV):H(viewS)+1);
    }];
}
-(void)initImagesView{
    imageEditorV=[[ImageSelectEditorView alloc] initWithFrame:CGRectMake(0, tempY, kScreenWidth, 10)];
    [scrollBG addSubview:imageEditorV];
    imageEditorV.maxNumber=9;
    
    
}
-(UITextField *)addViewWithFrameY:(float)_fy withTitle:(NSString *)strTitle withSel:(SEL)sel{
    UIView *viewBG=[[UIView alloc] initWithFrame:CGRectMake(0, _fy, kScreenWidth, 44)];
    viewBG.backgroundColor=[UIColor whiteColor];
    [scrollBG addSubview:viewBG];
    
    [viewBG addSubview:[RHMethods labelWithFrame:CGRectMake(10, 0, 80, H(viewBG)) font:fontTitle color:rgbTitleColor text:strTitle]];
    
    //    [viewBG addLineLayerWhitframe:CGRectMake(0, 0, W(viewBG), 0.5) whitColor:rgbLineColor];
    [viewBG addLineLayerWhitframe:CGRectMake(0, 43.5, W(viewBG), 0.5) whitColor:rgbLineColor];
    UITextField *txt=[RHMethods textFieldlWithFrame:CGRectMake(90, 0, W(viewBG)-100, 44) font:fontTitle color:rgbTitleColor placeholder:@"请输入" text:@"" ];
    [txt setTextAlignment:NSTextAlignmentRight];
    txt.delegate=self;
    [viewBG addSubview:txt];
    if (sel) {
        txt.placeholder=@"请选择";
        txt.enabled=NO;
        txt.frameWidth=W(viewBG)-117;
        UIButton *btnSelect=[RHMethods buttonWithFrame:viewBG.bounds title:nil image:@"Righticon070116" bgimage:nil];
        [btnSelect setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [btnSelect setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
        [btnSelect addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
        [viewBG addSubview:btnSelect];
    }
    return txt;
}
#pragma mark button
-(void)subButtonClicked{
    /*
     UITextField *txtName;//姓名
     UITextField *txtTel;
     UITextField *txtWx;
     UITextField *txtEmail;
     UITextField *txtCompany;
     UITextField *txtCode;
     */
    NSString *strName=txtName.text;
    NSString *strTel=txtTel.text;
    NSString *strWx=txtWx.text;
    if (![strName notEmptyOrNull]) {
        [SVProgressHUD showImage:nil status:@"请输入姓名"];
        return;
    }
    if (![strTel notEmptyOrNull]) {
        [SVProgressHUD showImage:nil status:@"请输入手机"];
        return;
    }
    if (![strWx notEmptyOrNull]) {
        [SVProgressHUD showImage:nil status:@"请输入微信"];
        return;
    }
    NSString *strNote=txtContent.text;
    krequestParam
    [dictparam setValue:strTel forKey:@"mobile"];
    [dictparam setValue:strName forKey:@"name"];
    [dictparam setValue:strWx forKey:@"wechat"];
    [dictparam setValue:strNote forKey:@"descr"];
    
    
    NSString *strUrl=XKFV4customerupdatecustomer;
    if ([self.userInfo isEqualToString:@"user"]||
        [self.userInfo isEqualToString:@"userEdit"]) {
        NSString *strEmail=txtEmail.text;
        NSString *strCompany=txtCompany.text;
        
        if (![strEmail notEmptyOrNull]) {
            [SVProgressHUD showImage:nil status:@"请输入邮箱"];
            return;
        }
        if (![strCompany notEmptyOrNull]) {
            [SVProgressHUD showImage:nil status:@"请输入公司名称"];
            return;
        }
        [dictparam setValue:strEmail forKey:@"email"];
        [dictparam setValue:strCompany forKey:@"companyname"];
    }else{
        NSString *strCode=txtCode.text;
        
        if (![strCode notEmptyOrNull]) {
            [SVProgressHUD showImage:nil status:@"请输入合同编号"];
            return;
        }
        [dictparam setValue:strCode forKey:@"contract"];
        strUrl=XKFV4customerupdateslip;
    }
    if (([self.userInfo isEqualToString:@"userEdit"] || [self.userInfo isEqualToString:@"sellEdit"]) ) {
        [dictparam setValue:[self.otherInfo valueForJSONStrKey:@"id"] forKey:@"id"];
        [dictparam setValue:[imageEditorV.arrayDeleteIds componentsJoinedByString:@","] forKey:@"pathid"];
    }
    NSArray *arrayImages=[imageEditorV getRequsetArrywithkey:@"path" removeServer:YES];
    [NetEngine uploadAllFileAction:strUrl withParams:dictparam fileArray:arrayImages onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            [self backRefreshData];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [super backButtonClicked:nil];
            });
        }else{
            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
        }
    } onError:^(NSError *error) {
        [SVProgressHUD showImage:nil status:alertErrorTxt];
    } withMask:SVProgressHUDMaskTypeBlack];
    
    
}
#pragma mark UITextFieldDelegate,UITextViewDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark selector
-(void)handleKeyboardDidShow:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
    
    [UIView animateWithDuration:0.3 animations:^{
        scrollBG.frame=CGRectMake(X(scrollBG), Y(scrollBG), W(scrollBG),self.view.frame.size.height- Y(scrollBG)-distanceToMove);
        
        
    }];
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        scrollBG.frame=CGRectMake(0, Y(scrollBG), kScreenWidth,kContentHeight-44);
        
        
    }];
}//
#pragma mark pop
-(void)backRefreshData{
    for (id controller in self.navigationController.viewControllers) {
        if ([NSStringFromClass([controller class]) isEqualToString:@"CRMViewController"]) {
            if ([(NSObject*)controller respondsToSelector:@selector(popRefreshData)]) {
                [controller performSelector:@selector(popRefreshData) withObject:nil afterDelay:0.01];
            }
            continue;
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
